package ipad.asia.controller;

import ipad.asia.config.FtpEnterprise;
import ipad.asia.entity.Enterprise;
import ipad.asia.entity.Shop;
import ipad.asia.service.enterprise.EnterpriseFeign;
import ipad.asia.service.shop.ShopFeign;
import ipad.asia.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
public class UserInfoController {
    @Autowired
    private EnterpriseFeign enterpriseFeign;
    @Autowired
    private ShopFeign shopFeign;
    @Autowired
    private MD5Util md5Util;

    @RequestMapping("/userInfo")
    public String userInfo(
                           @RequestParam(value = "phone",required = false) String phone,
                           @RequestParam(value = "email",required = false) String email,
                           @RequestParam(value = "sex",required = false) String sex,
                           @RequestParam(value = "address",required = false) String address,
                           @RequestParam(value = "credentials",required = false) String credentials,
                           @RequestParam(value = "password",required = false) String password,
                           HttpServletRequest request, Model model) throws IOException {
        System.out.println("password=========="+password);
        if(password!=null){
            HttpSession session=request.getSession();
            Enterprise enterprise = enterpriseFeign.getName((String) session.getAttribute("user"));
            Shop shop=shopFeign.findById((Integer) session.getAttribute("shopId"));
        enterpriseFeign.updateInfo((String) session.getAttribute("user"),
                phone, email, sex, address, credentials, MD5Util.inputPassToFormPass(password));
        model.addAttribute("enterprise",enterprise);
        model.addAttribute("shop",shop);
        return "userInfo";}
        else {
            HttpSession session=request.getSession();
            Enterprise enterprise = enterpriseFeign.getName((String) session.getAttribute("user"));
            Shop shop=shopFeign.findById((Integer) session.getAttribute("shopId"));
            model.addAttribute("enterprise",enterprise);
            model.addAttribute("shop",shop);
            return "userInfo";
        }
    }
}
